package com.zhou.combine.security.util.fileDown;

import com.github.liaochong.myexcel.core.DefaultStreamExcelBuilder;
import com.github.liaochong.myexcel.utils.AttachmentExportUtil;
import com.zhou.combine.security.util.fileDown.pojo.ArtCrowd;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * DefaultStreamExcelBuilderExampleController
 *
 * @author zhouxianfeng
 * @date 2021-3-31
 */
public class DefaultStreamExcelBuilderExampleController {

    private ExecutorService executorService = Executors.newFixedThreadPool(10);

    @GetMapping("/default/excel/stream/example")
    public void streamBuild(HttpServletResponse response) throws Exception {
        try (DefaultStreamExcelBuilder<ArtCrowd> defaultExcelBuilder = DefaultStreamExcelBuilder.of(ArtCrowd.class)
                .threadPool(executorService)
                .start()) {
            for (int i = 0; i < 100; i++) {
                // defaultExcelBuilder.append(this.getDataList());
                defaultExcelBuilder.asyncAppend(this::getDataList);
            }
            Workbook workbook = defaultExcelBuilder.build();
            AttachmentExportUtil.export(workbook, "艺术生信息1", response);
        }
    }

    @GetMapping("/default/excel/stream/continue/example")
    public void streamBuildWithContinue(HttpServletResponse response) throws Exception {
        DefaultStreamExcelBuilder<ArtCrowd> defaultExcelBuilder = DefaultStreamExcelBuilder.of(ArtCrowd.class)
                .threadPool(executorService)
                .start();
        for (int i = 0; i < 100; i++) {
            defaultExcelBuilder.asyncAppend(this::getDataList);
        }
        Workbook workbook = defaultExcelBuilder.build();

        DefaultStreamExcelBuilder<ArtCrowd> defaultStreamExcelBuilder = DefaultStreamExcelBuilder.of(ArtCrowd.class, workbook)
                .threadPool(executorService)
                .sheetName("sheet2")
                .start();
        for (int i = 0; i < 100; i++) {
            defaultStreamExcelBuilder.asyncAppend(this::getDataList);
        }
        workbook = defaultStreamExcelBuilder.build();
        AttachmentExportUtil.export(workbook, "艺术生信息", response);
    }

    private List<ArtCrowd> getDataList() {
        List<ArtCrowd> dataList = new ArrayList<>(1000);
        for (int i = 0; i < 1000; i++) {
            ArtCrowd artCrowd = new ArtCrowd();
            if (i % 2 == 0) {
                artCrowd.setName("Tom");
                artCrowd.setAge(19);
                artCrowd.setGender("Man");
                artCrowd.setPaintingLevel("一级证书");
                artCrowd.setDance(false);
                artCrowd.setAssessmentTime(LocalDateTime.now());
                artCrowd.setHobby("摔跤");
            } else {
                artCrowd.setName("Marry");
                artCrowd.setAge(18);
                artCrowd.setGender("Woman");
                artCrowd.setPaintingLevel("一级证书");
                artCrowd.setDance(true);
                artCrowd.setAssessmentTime(LocalDateTime.now());
                artCrowd.setHobby("钓鱼");
            }
            dataList.add(artCrowd);
        }
        return dataList;
    }


    /*public void download(QueryStatReqVO vo, HttpServletResponse response) throws IOException {

        LocalDate startDate = vo.getStartDate() == null ? LocalDate.now() : vo.getStartDate();
        LocalDate endDate = vo.getEndDate() == null ? LocalDate.now().plusDays(5) : vo.getEndDate();

        Workbook workbook = null;
        for (; startDate.compareTo(endDate) <= 0; startDate = startDate.plusDays(1)) {
            int i = 0;
            IPage<StatVo> page = new Page<>(++i, 100);
            QueryStatVo statVo = new QueryStatVo();
            statVo.setStatTime(startDate.toString());
            statVo.setChannel(vo.getChannel());
            page = buriedDetailMapper.stat1(page, statVo);
            List<CompletableFuture> futures = new ArrayList<>();
            DefaultStreamExcelBuilder defaultExcelBuilder;
            if (workbook == null) {
                defaultExcelBuilder = DefaultStreamExcelBuilder.of(StatVo.class);
            } else {
                defaultExcelBuilder = DefaultStreamExcelBuilder.of(StatVo.class, workbook);
            }
            defaultExcelBuilder.threadPool(executorService)
                    .sheetName(startDate.toString())
                    .start();
            int count = 0;
            do {
                count = page.getRecords().size();
                final List<StatVo> records = page.getRecords();
                CompletableFuture future = CompletableFuture.runAsync(() -> {
                    //数据追加
                    defaultExcelBuilder.append(records);
                });
                futures.add(future);
                page = new Page<>(++i, 100);
                page = buriedDetailMapper.stat1(page, statVo);
            }
            while (count == 100);
            futures.forEach(CompletableFuture::join);
            // 最终构建
            workbook = defaultExcelBuilder.build();
        }
        AttachmentExportUtil.export(workbook, "埋点数据", response);
    }*/
}
